package neusoft.edu.cn.recycledemo505js.activity.model;



import neusoft.edu.cn.recycledemo505js.activity.bean.UserBean;
import neusoft.edu.cn.recycledemo505js.activity.iface.LoginListener;
import neusoft.edu.cn.recycledemo505js.activity.service.LoginService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by del l on 2017/5/28.
 */

public class LoginModel {

    private Retrofit retrofit;
    private static String BASEURL = "http://amicool.neusoft.edu.cn/";
    final static String PREFNAME = "pref_login";

    public LoginModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void login(String userName, String passWord, final LoginListener listener) {
        LoginService service = retrofit.create(LoginService.class);
       Call<UserBean> call=service.Login(userName,passWord);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.body()!=null&&response.isSuccessful())
                    listener.onResponse(response.body());
                else{
                    listener.onFail("onResponse fail");
                }

            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                listener.onFail(t.toString());


            }
        });



    }
}